package com.example.csmall.product.service.impl;

import com.example.csmall.product.ex.ServiceException;
import com.example.csmall.product.mapper.BrandMapper;
import com.example.csmall.product.pojo.dto.BrandAddNewDTO;
import com.example.csmall.product.pojo.dto.BrandUpdateDTO;
import com.example.csmall.product.pojo.entity.Brand;
import com.example.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import com.example.csmall.product.pojo.vo.BrandListItemVO;
import com.example.csmall.product.pojo.vo.BrandStandardVO;
import com.example.csmall.product.repository.BrandCacheRepository;
import com.example.csmall.product.service.BrandService;
import com.example.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;

    @Autowired
    BrandCacheRepository brandCacheRepository;

    @Override
    public void addNew(BrandAddNewDTO brandAddNewDTO) {
        int countByName = brandMapper.countByName(brandAddNewDTO.getName());
        if(countByName>0){
            String message = "您输入的品牌名:"+brandAddNewDTO.getName()+"已经存在";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandAddNewDTO,brand);
        brandMapper.insert(brand);
    }

    @Override
    public void delete(Long id) {
        BrandStandardVO standardById = brandMapper.getStandardById(id);
        if(standardById!=null){
            brandMapper.deleteById(id);
        }else{
            String message = "您想要删除的数据已不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
    }

    @Override
    public void updateInfoById(Long id, BrandUpdateDTO brandUpdateDTO) {
        int byNameAndNotId = brandMapper.countByNameAndNotId(id, brandUpdateDTO.getName());
        if(byNameAndNotId>0){
            String message = "您所输入的品牌名称不可以和其他品牌名称重复";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        //创建对应表的类型对象
        Brand brand = new Brand();
        //将brandUpdateDTO中的信息copy到brand中
        BeanUtils.copyProperties(brandUpdateDTO,brand);
        brand.setId(id);
//        brand.setSales();
//        brand.setProductCount();
//        brand.setCommentCount();
//        brand.setPositiveCommentCount();
        brandMapper.updateInfoById(brand);
    }

    @Override
    public void setEnable(Long id) {
        //参数1 表示启用此品牌
        updateEnableById(id,1);
    }

    @Override
    public void setDisable(Long id) {
        //参数1 表示禁用此品牌
        updateEnableById(id,0);
    }

    /**
     * 调用此方法实现对品牌的请用状态进行更改
     * @param id
     * @param enable
     */
    private void updateEnableById(Long id,Integer enable){
        //判断当前id数据是否存在
        BrandStandardVO standardById = brandMapper.getStandardById(id);
        String message = "";
        if(standardById == null){
            message=ENABLE_TEXT[enable]+"品牌失败，所访问的品牌不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //判断当前id的启用状态和传入的状态是否相同
        if(enable.equals(standardById.getEnable())){
            message=ENABLE_TEXT[enable]+"品牌失败，目标状态已经是:"+ENABLE_TEXT[enable];
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        //创建修改类，对数据进行存放
        Brand brand = new Brand();
        brand.setId(id);
        brand.setEnable(enable);
        //执行mapper方法进行修改
        int rows = brandMapper.updateInfoById(brand);
        if(rows!=1){
            message = ENABLE_TEXT[enable]+"品牌失败，服务器异常，请稍后重试";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public List<BrandListItemVO> list() {
        //List<BrandListItemVO> list = brandMapper.list();
        List<BrandListItemVO> list = brandCacheRepository.list();
        return list;
    }

    /**
     * 重建缓存
     */
    @Override
    public void rebuildCache() {
        brandCacheRepository.delete();
        log.debug("调用BrandCacheRepository中的delete方法清除redis中已经缓存的brandList缓存数据");
        brandCacheRepository.save(brandMapper.list());
        log.debug("调用BrandCacheRepository中的list方法加载brandList缓存数据并且存入到redis中");
    }

    @Override
    public BrandStandardVO getStandardById(Long id) {
        BrandStandardVO standardById = brandMapper.getStandardById(id);
        if(standardById == null){
            String message = "您想要查询的数据已不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return standardById;
    }

}
